//
//  XJHImageView.swift
//  PublicPod
//
//  Created by 许俊华 on 2021/9/7.
//

import UIKit
import SDWebImage

public typealias ImageViewClickBlock = () -> Void

public class XJHImageView: UIImageView {

    // 圆角
    fileprivate var _cornerSize:CGFloat?
    public var cornerSize:CGFloat {
        set {
            _cornerSize = newValue
            self.layer.cornerRadius = _cornerSize ?? 0
            self.layer.masksToBounds = true
        }
        get {
            return _cornerSize ?? 0.0
        }
    }
    
    public var mImageViewClickBlock:ImageViewClickBlock?
    
    public class func imageWithImageName(imageName:String) -> XJHImageView {
        let iv = XJHImageView.init()
        if !XJHUtils.isEmpty(value: imageName as AnyObject) {
            iv.image = UIImage.init(named: imageName)
        }
        
        //点击事件
        let tap = UITapGestureRecognizer.init(target: iv, action: #selector(clickAction))
        //绑定tap
        iv.addGestureRecognizer(tap)
        
        return iv
    }
    
    // MARK: 点击事件
    @objc fileprivate func clickAction() -> Void {
        if self.mImageViewClickBlock != nil {
            self.mImageViewClickBlock!()
        }
    }
    
    // MARK: 加载网络图片
    public func loadImageWithUrlString(urlString:String) -> Void {
        autoreleasepool {
            self.sd_setImage(with: URL.init(string: urlString), placeholderImage: nil)
        }
        
    }
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
